package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.FriendBean;
import cn.innosmart.aq.bean.NearByPeople;
import cn.innosmart.aq.util.LocationUtil;
import cn.innosmart.aq.view.activity.FriendActivity;

/**
 * Created by David on 2015/9/15.
 */
public class NearByPeopleAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<NearByPeople> nearByPeoples;
    private View.OnClickListener mAddOnClickListener;

    /**
     * 构造函数
     *
     * @param context
     * @param nearByPeoples 附近的人列表
     */
    public NearByPeopleAdapter(Context context, ArrayList<NearByPeople> nearByPeoples) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.nearByPeoples = nearByPeoples;
    }

    @Override
    public int getCount() {
        return nearByPeoples.size();
    }

    @Override
    public Object getItem(int position) {
        return nearByPeoples.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_nearby_people, null);
            holder.tvNickname = (TextView) convertView.findViewById(R.id.tv_nickname);
            holder.tvLocation = (TextView) convertView.findViewById(R.id.tv_location);
            holder.tvHeadIcon = (ImageView) convertView.findViewById(R.id.tv_headIcon);
            holder.ivAddFriend = (ImageView) convertView.findViewById(R.id.iv_add_friend);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        NearByPeople nearByPeople = nearByPeoples.get(position);
        float dis = nearByPeople.getDistance(LocationUtil.getInstance().latitude, LocationUtil.getInstance().lontitude);
        Float distance = new Float(dis);
        int extractDistance = distance.intValue();
        String strs;
        if (distance < 1000) {
            strs = String.format(context.getString(R.string.nearby_people_distance), "1");
            ;
        } else {
            String str = String.valueOf(extractDistance / 1000);
            strs = String.format(context.getString(R.string.nearby_people_distance), str);
        }
        holder.tvLocation.setText(strs);
        holder.tvNickname.setText(nearByPeople.getNickName());
        if (checkIfExistFriend(nearByPeople)) {
            holder.ivAddFriend.setImageResource(R.drawable.chat);
        } else {
            holder.ivAddFriend.setImageResource(R.drawable.boxlist_addbox);
        }
//
        return convertView;
    }

    //检查附近的人是否已经是好友
    private boolean checkIfExistFriend(NearByPeople nearByPeople) {
        Iterator iter = FriendActivity.childs.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            ArrayList<FriendBean> friendBeans = (ArrayList<FriendBean>) entry.getValue();
            for (int i = 0; i < friendBeans.size(); i++) {
                if (friendBeans.get(i).getId().equals(nearByPeople.getId())) {
                    nearByPeople.setIsFriend(true);
                    return true;
                }
            }
        }
        nearByPeople.setIsFriend(false);
        return false;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    /**
     * 刷新数据
     */
    public void refreshData() {
        notifyDataSetChanged();
    }

    public void setAddOnClickListener(View.OnClickListener mAddOnClickListener) {
        this.mAddOnClickListener = mAddOnClickListener;
    }

    static class ViewHolder {
        ImageView tvHeadIcon;
        TextView tvNickname;
        TextView tvLocation;
        ImageView ivAddFriend;
    }
}
